终于我的贪心过了,方法与题解不同,我每次记录历史最高值h,若当前x<=h则可以,然后判定几个特殊条件,AC了。
高山算法是什么?自行百度,本人无厘头命名,貌似是时间空间最优的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int N = 100005; struct data{ long long x,y; bool operator < (const data &rhs)const{ return x<rhs.x||(x==rhs.x&&(y-x)>(rhs.y-rhs.x)); } }a[N]; inline void read(long long &res){ static char ch;long long flag=1; while((ch=getchar())<'0'||ch>'9')if(ch=='-')flag=-1;res=ch-48; while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-48;res*=flag; } long long t,n,w,h,flag,b; int main(){ freopen("backpack.in","r",stdin); freopen("backpack.out","w",stdout); read(t); while(t--){ read(n),read(w);h=w; for(register int i=1;i<=n;i++)read(a[i].x),read(a[i].y); sort(a+1,a+1+n),flag=1;b=0x3f3f3f3f; for(register int i=1;i<=n;i++){ if(h<a[i].x){cout<<"No"<<endl;flag=0;break;} w-=a[i].x;w+=a[i].y; h=max(h,w);b=min(b,a[i].y); } if(flag){ if(w<b)cout<<"No"<<endl; else cout<<"Yes"<<endl; } } return 0; }
|